import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    Socket socket = null;
    public TcpClient(String serverIp, int port) throws IOException {
        //创建的同时和服务器建立连接
        socket = new Socket(serverIp,port);
    }

    public void start(){

        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter writer = new PrintWriter(outputStream);
            Scanner in = new Scanner(inputStream);
            while (true){
                //1.读取请求
                System.out.print("-> ");
                String req = scanner.next();
                //2.发送请求
                writer.println(req);
                writer.flush();
                //3.读取响应
                String res = in.next();
                //4.显示
                System.out.println(res);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpClient tcpClient = new TcpClient("172.29.16.1",9090);
        tcpClient.start();
    }
}
